home *** CD-ROM | disk | FTP | other *** search
/ CD Actual Thematic 7: Programming / CDAT7.iso / demos / VisualAge for Java 2.0 Entry / setup / data1.cab / ide-e / IDE / cache / PMZARN (.txt) < prev    next >
Encoding:
Java Class File  |  1998-09-16  |  10.7 KB  |  356 lines

  1. package com.sun.java.swing.plaf.basic;
  2.  
  3. import com.sun.java.swing.CellRendererPane;
  4. import com.sun.java.swing.JComponent;
  5. import com.sun.java.swing.JTable;
  6. import com.sun.java.swing.LookAndFeel;
  7. import com.sun.java.swing.SwingUtilities;
  8. import com.sun.java.swing.plaf.ComponentUI;
  9. import com.sun.java.swing.plaf.TableHeaderUI;
  10. import com.sun.java.swing.table.JTableHeader;
  11. import com.sun.java.swing.table.TableCellRenderer;
  12. import com.sun.java.swing.table.TableColumn;
  13. import com.sun.java.swing.table.TableColumnModel;
  14. import java.awt.Color;
  15. import java.awt.Component;
  16. import java.awt.Cursor;
  17. import java.awt.Dimension;
  18. import java.awt.Graphics;
  19. import java.awt.Point;
  20. import java.awt.Rectangle;
  21. import java.awt.event.FocusEvent;
  22. import java.awt.event.FocusListener;
  23. import java.awt.event.InputEvent;
  24. import java.awt.event.MouseEvent;
  25. import java.awt.event.MouseListener;
  26. import java.awt.event.MouseMotionListener;
  27. import java.io.Serializable;
  28. import java.util.Enumeration;
  29.  
  30. public class BasicTableHeaderUI extends TableHeaderUI implements MouseListener, MouseMotionListener, FocusListener, Serializable {
  31.    protected JTableHeader header;
  32.    protected CellRendererPane rendererPane = new CellRendererPane();
  33.    protected transient int hitColumnIndex;
  34.    protected transient TableColumn hitColumn;
  35.    protected transient boolean isResizing;
  36.    protected transient int originalWidth;
  37.    protected transient int widthDelta;
  38.    protected transient int lastMouseX;
  39.    protected transient int realDraggedDistance;
  40.    protected transient boolean isReordering;
  41.    protected transient boolean okToReorder;
  42.    protected transient boolean fireAction;
  43.    protected transient boolean hasPress = false;
  44.    static final Cursor defaultCursor = Cursor.getPredefinedCursor(0);
  45.    static final Cursor resizeCursor = Cursor.getPredefinedCursor(11);
  46.  
  47.    public static ComponentUI createUI(JComponent h) {
  48.       return new BasicTableHeaderUI();
  49.    }
  50.  
  51.    public void focusGained(FocusEvent e) {
  52.    }
  53.  
  54.    public void focusLost(FocusEvent e) {
  55.    }
  56.  
  57.    public Dimension getMaximumSize(JComponent c) {
  58.       return this.getPreferredSize(c);
  59.    }
  60.  
  61.    public Dimension getMinimumSize(JComponent c) {
  62.       return this.getPreferredSize(c);
  63.    }
  64.  
  65.    public Dimension getPreferredSize(JComponent c) {
  66.       JTableHeader header = (JTableHeader)c;
  67.       JTable table = header.getTable();
  68.       Dimension tableSize = ((JComponent)table).getPreferredSize();
  69.       Dimension size = new Dimension();
  70.       size.width = tableSize.width;
  71.       int column = 0;
  72.  
  73.       for(Enumeration enumeration = header.getColumnModel().getColumns(); enumeration.hasMoreElements(); ++column) {
  74.          TableColumn aColumn = (TableColumn)enumeration.nextElement();
  75.          TableCellRenderer renderer = aColumn.getHeaderRenderer();
  76.          Component comp = renderer.getTableCellRendererComponent(header.getTable(), aColumn.getHeaderValue(), false, false, -1, column);
  77.          size.height = Math.max(size.height, comp.getPreferredSize().height);
  78.       }
  79.  
  80.       return size;
  81.    }
  82.  
  83.    private int getResizingColumn(Point p) {
  84.       int column = 0;
  85.       Rectangle resizeRect = new Rectangle(-3, 0, 6, this.header.getSize().height);
  86.       int columnMargin = this.header.getColumnModel().getColumnMargin();
  87.  
  88.       for(Enumeration enumeration = this.header.getColumnModel().getColumns(); enumeration.hasMoreElements(); ++column) {
  89.          TableColumn aColumn = (TableColumn)enumeration.nextElement();
  90.          resizeRect.x += aColumn.getWidth() + columnMargin;
  91.          if (resizeRect.x > p.x) {
  92.             break;
  93.          }
  94.  
  95.          if (resizeRect.contains(p)) {
  96.             return column;
  97.          }
  98.       }
  99.  
  100.       return -1;
  101.    }
  102.  
  103.    public void installUI(JComponent c) {
  104.       this.header = (JTableHeader)c;
  105.       this.header.add(this.rendererPane);
  106.       ((Component)c).addMouseListener(this);
  107.       ((Component)c).addMouseMotionListener(this);
  108.       ((Component)c).addFocusListener(this);
  109.       LookAndFeel.installColorsAndFont(this.header, "TableHeader.background", "TableHeader.foreground", "TableHeader.font");
  110.    }
  111.  
  112.    public void mouseClicked(MouseEvent e) {
  113.    }
  114.  
  115.    public void mouseDragged(MouseEvent e) {
  116.       this.fireAction = false;
  117.       int mouseX = e.getX();
  118.       if (mouseX != this.lastMouseX) {
  119.          if (this.isResizing) {
  120.             this.widthDelta += mouseX - this.lastMouseX;
  121.             int newWidth = this.originalWidth + this.widthDelta;
  122.             this.hitColumn.setWidth(newWidth);
  123.             this.header.revalidate();
  124.             this.header.repaint();
  125.             if (this.header.getUpdateTableInRealTime()) {
  126.                JTable table = this.header.getTable();
  127.                ((JComponent)table).revalidate();
  128.                ((Component)table).repaint();
  129.             }
  130.          } else if (this.okToReorder || this.isReordering) {
  131.             this.isReordering = true;
  132.             this.move(e);
  133.          }
  134.  
  135.          this.lastMouseX = mouseX;
  136.       }
  137.    }
  138.  
  139.    public void mouseEntered(MouseEvent e) {
  140.    }
  141.  
  142.    public void mouseExited(MouseEvent e) {
  143.    }
  144.  
  145.    public void mouseMoved(MouseEvent e) {
  146.       if (this.getResizingColumn(e.getPoint()) != -1) {
  147.          if (this.header.getCursor() != resizeCursor) {
  148.             this.header.setCursor(resizeCursor);
  149.          }
  150.       } else if (this.header.getCursor() != defaultCursor) {
  151.          this.header.setCursor(defaultCursor);
  152.       }
  153.  
  154.    }
  155.  
  156.    public void mousePressed(MouseEvent e) {
  157.       if (!this.hasPress) {
  158.          this.hasPress = true;
  159.          TableColumnModel columnModel = this.header.getColumnModel();
  160.          int index = 0;
  161.          this.isReordering = false;
  162.          this.isResizing = false;
  163.          this.fireAction = true;
  164.          this.hitColumnIndex = -1;
  165.          this.hitColumn = null;
  166.          Point p = e.getPoint();
  167.          if ((index = columnModel.getColumnIndexAtX(p.x)) != -1) {
  168.             Rectangle headerRect = this.header.getHeaderRect(index);
  169.             int resizeIndex = this.getResizingColumn(p);
  170.             if (this.header.getResizingAllowed() && resizeIndex != -1) {
  171.                this.hitColumn = columnModel.getColumn(resizeIndex);
  172.                if (this.hitColumn.getResizable()) {
  173.                   this.isResizing = true;
  174.                   this.fireAction = false;
  175.                   this.hitColumnIndex = resizeIndex;
  176.                   this.originalWidth = this.hitColumn.getWidth();
  177.                   this.widthDelta = 0;
  178.                   this.header.setResizingColumn(this.hitColumn);
  179.                   this.lastMouseX = p.x;
  180.                   this.hitColumn.disableResizedPosting();
  181.                } else {
  182.                   this.hitColumn = null;
  183.                }
  184.             } else {
  185.                boolean controlKeyDown = ((InputEvent)e).isControlDown();
  186.                if (this.header.getReorderingAllowed() && headerRect.contains(p)) {
  187.                   this.okToReorder = columnModel.getColumnCount() > 1;
  188.                   if (controlKeyDown) {
  189.                      this.okToReorder = false;
  190.                      this.fireAction = false;
  191.                   }
  192.                }
  193.  
  194.                JTable table = this.header.getTable();
  195.                if (columnModel.getColumnSelectionAllowed() && headerRect.contains(p) && table != null) {
  196.                   if (controlKeyDown) {
  197.                      if (table.isColumnSelected(index)) {
  198.                         table.removeColumnSelectionInterval(index, index);
  199.                      } else {
  200.                         table.addColumnSelectionInterval(index, index);
  201.                      }
  202.                   } else {
  203.                      table.setColumnSelectionInterval(index, index);
  204.                   }
  205.                }
  206.  
  207.                this.hitColumnIndex = index;
  208.                this.header.setDraggedColumn(this.hitColumn);
  209.                this.header.setDraggedDistance(0);
  210.                this.lastMouseX = p.x;
  211.                this.realDraggedDistance = 0;
  212.             }
  213.          }
  214.  
  215.       }
  216.    }
  217.  
  218.    public void mouseReleased(MouseEvent e) {
  219.       this.hasPress = false;
  220.       if (this.isResizing) {
  221.          this.isResizing = false;
  222.          this.originalWidth = 0;
  223.          this.header.setResizingColumn((TableColumn)null);
  224.          int newWidth = this.hitColumn.getWidth();
  225.          this.hitColumn.setWidth(this.originalWidth);
  226.          this.hitColumn.enableResizedPosting();
  227.          this.hitColumn.setWidth(newWidth);
  228.       } else if (this.isReordering) {
  229.          this.isReordering = false;
  230.          this.originalWidth = this.widthDelta = 0;
  231.          this.realDraggedDistance = 0;
  232.       } else if (this.fireAction) {
  233.          this.fireAction = false;
  234.       }
  235.  
  236.       this.header.setDraggedColumn((TableColumn)null);
  237.       this.header.setDraggedDistance(0);
  238.       this.header.repaint();
  239.       JTable table = this.header.getTable();
  240.       if (table != null) {
  241.          ((Component)table).repaint();
  242.       }
  243.  
  244.       this.okToReorder = false;
  245.       this.hitColumnIndex = -1;
  246.       this.hitColumn = null;
  247.    }
  248.  
  249.    private void move(MouseEvent e) {
  250.       TableColumnModel columnModel = this.header.getColumnModel();
  251.       int lastColumn = columnModel.getColumnCount() - 1;
  252.       Rectangle redrawRect = this.header.getHeaderRect(this.hitColumnIndex);
  253.       redrawRect.x += this.header.getDraggedDistance();
  254.       int delta = e.getX() - this.lastMouseX;
  255.       this.realDraggedDistance += delta;
  256.       Rectangle redrawRect2 = this.header.getHeaderRect(this.hitColumnIndex);
  257.       redrawRect2.x += this.realDraggedDistance;
  258.       redrawRect = redrawRect.union(redrawRect2);
  259.       if (this.realDraggedDistance < 0 && this.hitColumnIndex != 0) {
  260.          int var10 = columnModel.getColumnMargin() + columnModel.getColumn(this.hitColumnIndex - 1).getWidth();
  261.          if (-this.realDraggedDistance > var10 / 2) {
  262.             columnModel.moveColumn(this.hitColumnIndex, this.hitColumnIndex - 1);
  263.             this.realDraggedDistance += var10;
  264.             --this.hitColumnIndex;
  265.          }
  266.       } else if (this.realDraggedDistance > 0 && this.hitColumnIndex != lastColumn) {
  267.          int width = columnModel.getColumnMargin() + columnModel.getColumn(this.hitColumnIndex + 1).getWidth();
  268.          if (this.realDraggedDistance > width / 2) {
  269.             columnModel.moveColumn(this.hitColumnIndex, this.hitColumnIndex + 1);
  270.             this.realDraggedDistance = -(width - this.realDraggedDistance);
  271.             ++this.hitColumnIndex;
  272.          }
  273.       }
  274.  
  275.       int draggedDistance = this.realDraggedDistance;
  276.       if (this.hitColumnIndex == 0) {
  277.          if (draggedDistance < 0) {
  278.             draggedDistance = 0;
  279.          }
  280.       } else if (this.hitColumnIndex == lastColumn && draggedDistance > 0) {
  281.          draggedDistance = 0;
  282.       }
  283.  
  284.       this.header.setDraggedColumn(columnModel.getColumn(this.hitColumnIndex));
  285.       this.header.setDraggedDistance(draggedDistance);
  286.       this.header.repaint(redrawRect.x, 0, redrawRect.width, redrawRect.height);
  287.       if (this.header.getUpdateTableInRealTime()) {
  288.          JTable table = this.header.getTable();
  289.          if (table != null) {
  290.             ((Component)table).repaint(redrawRect.x, 0, redrawRect.width, (table.getRowHeight() + table.getIntercellSpacing().height) * table.getRowCount());
  291.          }
  292.       }
  293.  
  294.    }
  295.  
  296.    public void paint(Graphics g, JComponent c) {
  297.       JTableHeader header = (JTableHeader)c;
  298.       Rectangle paintBounds = g.getClipBounds();
  299.       Dimension size = ((Component)header).getSize();
  300.       Component component = null;
  301.       if (header.getColumnModel() != null) {
  302.          Color bColor = ((Component)header).getParent().getBackground();
  303.          if (bColor != null) {
  304.             g.setColor(bColor);
  305.             g.fillRect(paintBounds.x, paintBounds.y, paintBounds.width, paintBounds.height);
  306.          }
  307.  
  308.          Rectangle cellRect = new Rectangle(0, 0, size.width, size.height);
  309.          int column = 0;
  310.          boolean drawn = false;
  311.          Rectangle draggedCellRect = null;
  312.          TableColumn draggedColumnObject = null;
  313.          int columnMargin = header.getColumnModel().getColumnMargin();
  314.  
  315.          for(Enumeration enumeration = header.getColumnModel().getColumns(); enumeration.hasMoreElements(); ++column) {
  316.             TableColumn aColumn = (TableColumn)enumeration.nextElement();
  317.             cellRect.width = aColumn.getWidth() + columnMargin;
  318.             if (cellRect.intersects(paintBounds)) {
  319.                drawn = true;
  320.                if (aColumn != header.getDraggedColumn()) {
  321.                   TableCellRenderer renderer = aColumn.getHeaderRenderer();
  322.                   component = renderer.getTableCellRendererComponent(header.getTable(), aColumn.getHeaderValue(), false, false, -1, column);
  323.                   this.rendererPane.add(component);
  324.                   this.rendererPane.paintComponent(g, component, header, cellRect.x, cellRect.y, cellRect.width, cellRect.height, true);
  325.                } else {
  326.                   g.setColor(((Component)header).getParent().getBackground());
  327.                   g.fillRect(cellRect.x, cellRect.y, cellRect.width, cellRect.height);
  328.                   draggedCellRect = new Rectangle(cellRect);
  329.                }
  330.             } else if (drawn) {
  331.                break;
  332.             }
  333.  
  334.             cellRect.x += cellRect.width;
  335.          }
  336.  
  337.          draggedColumnObject = header.getDraggedColumn();
  338.          if (draggedColumnObject != null && draggedCellRect != null) {
  339.             TableCellRenderer var20 = draggedColumnObject.getHeaderRenderer();
  340.             component = var20.getTableCellRendererComponent(header.getTable(), draggedColumnObject.getHeaderValue(), false, false, -1, column);
  341.             draggedCellRect.x += header.getDraggedDistance();
  342.             SwingUtilities.paintComponent(g, component, header, draggedCellRect);
  343.          }
  344.  
  345.       }
  346.    }
  347.  
  348.    public void uninstallUI(JComponent c) {
  349.       this.header.remove(this.rendererPane);
  350.       this.header = null;
  351.       ((Component)c).removeMouseListener(this);
  352.       ((Component)c).removeMouseMotionListener(this);
  353.       ((Component)c).removeFocusListener(this);
  354.    }
  355. }
  356.